package 简单题目;

public class 重复词 {

	public static void main(String[] args) {

		String a = "babababa";
		System.out.println(getLongest(a.length(), a));
	}

	// 得到所有前缀的最长重复词
	public static long getLongest(int n, String s) {
		long r = 0;
		for (int i = 0; i <= n; i++) {
			r += longestDump(s.substring(0, i)).length();
		}
		return r;
	}

	// 得到最长重复词
	public static String longestDump(String s) {
		String r = "";
		for (int i = 0; i <= s.length(); i++) {
			String sub = s.substring(0, i);
			if (isDump(sub, s) && sub.length() > r.length()) {
				r = sub;
			}
		}
		return r;
	}

	// 判断是不是重复词
	public static boolean isDump(String dump, String s) {
		if (isPre(dump, s).equals("isRealPre")
				&& (isPre(s, dump + dump).equals("isPre") || isPre(s, dump + dump).equals("isRealPre")))
			return true;
		return false;
	}

	// 判断前缀
	public static String isPre(String pre, String s) {
		String r = "";
		if (pre.equals(s))
			r = "isPre";
		if (!pre.equals(s) && s.indexOf(pre) == 0)
			r = "isRealPre";
		if (pre.indexOf(pre) == -1)
			r = "isNotPre";
		return r;
	}

}
